<?php
// 首页显示信息以及基本配置
namespace NewAdmin\Controller;
use Think\Controller;
class IndexController extends CommonController {
    function _initialize() {
        parent::_initialize();
       
    }

    function index(){
    	//今日订单详情
        $order=M('order');
        $today=today();
        $start_date = $today[0];
        $end_date = $today[1];
		$map['add_time']  = array('between',array($start_date,$end_date));
		$map['order_state']  = array('egt','20');
		$today_order=$order->where($map)->count();
		$today_price=$order->where($map)->sum('order_amount');
		//总店铺数
		$store=M('store');
		$count_store=$store->count();
		//总会员数
		$users=M('users');
		$count_users=$users->count();
		//订单统计
		//未付款
		$wpay_order=$order->where("order_state='10' and refund_state=0")->count();
		//已付款
		$ypay_order=$order->where("order_state='20' and refund_state=0")->count();
		//已发货
		$yfpay_order=$order->where("order_state='30' and refund_state=0")->count();
		//已签收
		$ycpay_order=$order->where("order_state='40' and refund_state=0")->count();
		//已评价
		$yepay_order=$order->where("order_state='40' and evaluation_state>0")->count();
		$wepay_order=$order->where("order_state='40' and evaluation_state=0")->count();
		//已关闭
		$close_order=$order->where("order_state='0'")->count();
		//退款中
		$back_order=$order->where("refund_state>0 and refund_state<3")->count();
		
        $this->assign('wpay_order',$wpay_order);
        $this->assign('ypay_order',$ypay_order);
        $this->assign('yfpay_order',$yfpay_order);
        $this->assign('ycpay_order',$ycpay_order);
        $this->assign('yepay_order',$yepay_order);
        $this->assign('wepay_order',$wepay_order);
        $this->assign('close_order',$close_order);
        $this->assign('back_order',$back_order);
        
        $this->assign('today_order',$today_order);        
        $this->assign('today_price',$today_price);
        $this->assign('count_store',$count_store);
        $this->assign('count_users',$count_users);
        $this->assign('title','首页');
  		$this->display();
    }
    
      /**
     * 订单 图表 数据
     */
    public function getOrderChartCount(){
		$data = S('data');
		if(!$data){
			 $month=month();
			$start=$month[0];
			$end=$month[1];
			$order=M('order');
			
			for($j=0;$j<($end+1-$start)/86400;$j++){
				$date_start = $start+86400*$j;
				$date_end = $start+86400*($j+1);
				$map['add_time']  = array('between',array($date_start,$date_end));
				$map['order_state']  = array('egt','20');
				$count = $order->cache(true)->where($map)->count();
				$data[$j] = array(
					(1+$j).'日', (int)$count
				);
			}
			S('data',$data,300);
		}
       
       $this->ajaxReturn($data);
    }
    public function getgoodssaleschartcount(){
		$data = S('data2');
		if(!$data){
			$month=month();
			$start=$month[0];
			$end=$month[1];
			$order=M('order');
			
			for($j=0;$j<($end+1-$start)/86400;$j++){
				$date_start = $start+86400*$j;
				$date_end = $start+86400*($j+1);
				$map['add_time']  = array('between',array($date_start,$date_end));
				$map['order_state']  = array('egt','20');
				$count = $order->cache(true)->where($map)->sum('order_amount');
				
				$data[0][$j] = array(
					(1+$j).'日'
				);
				$data[1][$j] = array(
					 (float)$count
				);
			}
			S('data2',$data,300);
		}
       $this->ajaxReturn($data);
    }
    public function getgoodscreatechartcount(){
		$data = S('data3');
		if(!$data){
			$month=month();
			$start=$month[0];
			$end=$month[1];
			$order=M('order');
			$shop=M('shop');
			
			for($j=0;$j<($end+1-$start)/86400;$j++){
				$date_start = $start+86400*$j;
				$date_end = $start+86400*($j+1);
				$map['addtime']  = array('between',array($date_start,$date_end));
				$goods_num = $shop->cache(true)->where($map)->count();
				
				
				$data[0][$j] = array(
					(1+$j).'日'
				);
				$data[1][$j] = array(
					 (float)$goods_num
				);
			}
			S('data3',$data,300);
		}
       $this->ajaxReturn($data);
    }
    
    public function getshopcreatenumchartcount(){
		$data = S('data4');
			if(!$data){
			$month=month();
			$start=$month[0];
			$end=$month[1];
			$order=M('order');
			$store=M('store');
			
			for($j=0;$j<($end+1-$start)/86400;$j++){
				$date_start = $start+86400*$j;
				$date_end = $start+86400*($j+1);
				$map['addtime']  = array('between',array($date_start,$date_end));
				$store_num = $store->cache(true)->where($map)->count();
				
				
				$data[0][$j] = array(
					(1+$j).'日'
				);
				$data[1][$j] = array(
					 (float)$store_num
				);
			}
			S('data4',$data,300);
		}
       $this->ajaxReturn($data);
    }
    public function getuserregchartcount(){
		$data = S('data5');
		if(!$data){
			$month=month();
			$start=$month[0];
			$end=$month[1];
			$users=M('users');
			for($j=0;$j<($end+1-$start)/86400;$j++){
				$date_start = $start+86400*$j;
				$date_end = $start+86400*($j+1);
				$map['addtime']  = array('between',array($date_start,$date_end));
				$user_num = $users->cache(true)->where($map)->count();
				
				
				$data[0][$j] = array(
					(1+$j).'日'
				);
				$data[1][$j] = array(
					 (float)$user_num
				);
			}
			S('data5',$data,300);
		}
       $this->ajaxReturn($data);
    }
    function getUserRegCount(){
		$result = S('result');
		if(!$result){
			$user=M('users');
			 //今日商品添加
			$today=today();
			$start_date = $today[0];
			$end_date = $today[1];
			//本日添加商品数
			$map['addtime']  = array('between',array($start_date,$end_date));
			$num_day = $user->cache(true)->where($map)->count();
			//昨天添加商品数
			$yesterday=yesterday();
			$start_date =$yesterday[0];
			$end_date =$yesterday[1];
			$map['addtime']  = array('between',array($start_date,$end_date));
			$num_yesterday = $user->cache(true)->where($map)->count();
			//本月
			$month=month();
			$start=$month[0];
			$end=$month[1];
			$map['addtime']  = array('between',array($start,$end));
			$num_month =$user->cache(true)->where($map)->count();
			//总计
			$num_count = $user->count();
			$result = array(
				"user_num_day"=>$num_day,
				"user_num_yesterday"=>$num_yesterday,
				"user_num_month"=>$num_month,
				"user_num_count"=>$num_count
			);
			S('result',$result,300);
		}
        $this->ajaxReturn($result);
    	   
    }
    function getshopcreatecount(){
		$result = S('result2');
		if(!$result){
			$shop=M('shop');
			 //今日商品添加
			$today=today();
			$start_date = $today[0];
			$end_date = $today[1];
			//本日添加商品数
			$map['addtime']  = array('between',array($start_date,$end_date));
			$num_day = $shop->cache(true)->where($map)->count();
			//昨天添加商品数
			$yesterday=yesterday();
			$start_date =$yesterday[0];
			$end_date =$yesterday[1];
			$map['addtime']  = array('between',array($start_date,$end_date));
			$num_yesterday = $shop->cache(true)->where($map)->count();
			//本月
			$month=month();
			$start=$month[0];
			$end=$month[1];
			$map['addtime']  = array('between',array($start,$end));
			$num_month =$shop->cache(true)->where($map)->count();
			//总计
			$num_count = $shop->count();
			$result = array(
				"num_day"=>$num_day,
				"num_yesterday"=>$num_yesterday,
				"num_month"=>$num_month,
				"num_count"=>$num_count
			);
			S('result2',$result,300);
		}
        $this->ajaxReturn($result);
    	   
    }
    
    function getshoreCreateCount(){
		$result = S('result3');
		if(!$result){
			$store=M('store');
			 //今日商品添加
			$today=today();
			$start_date = $today[0];
			$end_date = $today[1];
			//本日添加商品数
			$map['addtime']  = array('between',array($start_date,$end_date));
			$num_day = $store->cache(true)->where($map)->count();
			//昨天添加商品数
			$yesterday=yesterday();
			$start_date =$yesterday[0];
			$end_date =$yesterday[1];
			$map['addtime']  = array('between',array($start_date,$end_date));
			$num_yesterday = $store->cache(true)->where($map)->count();
			//本月
			$month=month();
			$start=$month[0];
			$end=$month[1];
			$map['addtime']  = array('between',array($start,$end));
			$num_month =$store->cache(true)->where($map)->count();
			//总计
			$num_count = $store->cache(true)->count();
			$result = array(
				"num_day"=>$num_day,
				"num_yesterday"=>$num_yesterday,
				"num_month"=>$num_month,
				"num_count"=>$num_count
			);
			S('result3',$result,300);
		}
        $this->ajaxReturn($result);
    	   
    }
    // 清除缓存
    public function clearcache(){
        $dir=RUNTIME_PATH;
        $this->deldir($dir);
        echo "0|||清除缓存成功!|||reload";
    }
	function deldir($dir) {
        
        $dh = opendir($dir);
        while ($file = readdir($dh)) {
            if ($file != "." && $file != "..") {
                $fullpath = $dir . "/" . $file;
                if (!is_dir($fullpath)) {
                    unlink($fullpath);
                } else {
                   $this->deldir($fullpath);
                }
            }
        }
    }

 
    
}